package org.vectrics.common.db;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

import javax.naming.*;

/**
 * 
 * @author Mike Moore
 *
 */
public class DbObject {
    private Logger log = Logger.getLogger(this.getClass());

    private Connection dbConnection = null;
    private DataSource datasource   = null;


    public void list(Context context, String parentName) {
        log.warn("List Context - Parent Name = " + parentName);
        try {
            NamingEnumeration ne = context.list("");
            while (ne.hasMoreElements()) {
                NameClassPair binding = (NameClassPair) ne.next();
                String name = binding.getName();
                log.warn("binding: " + name);
            }
        }
        catch (Exception ex) {
            log.error("Exception listing context.", ex);
        }
    }
    
    protected void findDataSourceFromJndi(String jndiName)
            throws javax.naming.NamingException {
        InitialContext ic = new InitialContext();
        //list(ic, "Root");
        //Context context = (Context)ic.lookup("java:comp");
        //list(context, "java:comp");
        
        setDataSource((DataSource)ic.lookup(jndiName));
    }

    protected String getSqlValue(String value) {
        String sqlValue;
        if (value == null)
            sqlValue = "null";
        else
            sqlValue = "'" + value.trim() + "'";
        return(sqlValue);
    }

    protected void setDataSource(DataSource source) {
        datasource = source;
    }

    
    protected Connection getDBConnection() {
        log.debug("getDbConnection() called");
        try {
            if (datasource == null) 
                log.error("datasource is null.");
            else {
                if (dbConnection != null) {
                    log.error("getDbConnection() - previous db connection not closed.  Now closing");
                    closeConnection();   
                }
                dbConnection = datasource.getConnection();
            }
        } catch (SQLException se) {
            log.error("getDBConnction() - Exception getting DB Connection", se);
            dbConnection = null;
        }
        return(dbConnection);
    }

    
    protected void closeResultSet(ResultSet rs)  {
        log.debug("closeResultSet() called");
        try {
            if (rs != null) {
                rs.close();
            }
        } catch(SQLException se) {

            log.error("SQLException while closing " +
                                          "result set ", se);
        }
    }

    protected void closeStatement(Statement stmt)  {
        log.debug("closeStatement() called");
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch(SQLException se) {
            log.error("SQLException while closing " +
                                          "statement ", se);
        }
    }

    protected boolean closeConnection()  {
        boolean success = false;
        log.debug("closeConnection() called");
        try {
            if (dbConnection != null && !dbConnection.isClosed()) {
                dbConnection.close();
                dbConnection = null;
                success = true;
            }
        } catch(SQLException se) {
            log.error("SQLException while closing " +
                                          "DB connection ", se);
        }
        return(success);
    }

}